﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Threading;
using System.Net;
using System.Xml.Serialization;

namespace raktajino
{
    [Serializable]
    public class UpdateConfig
    {
        private static UpdateConfig m_instance = null;
        string m_downloadUrl;
        Version m_serverVersion;
        string m_updateCommand;
        string m_autoUpdateCountdown;

        public static readonly Version LocalVersion = Assembly.GetExecutingAssembly().GetName().Version;

        public string UpdateCommand
        {
            get { return m_updateCommand; }
            set { m_updateCommand = value; }
        }
        public string DownloadUrl
        {
            get { return m_downloadUrl; }
            set { m_downloadUrl = value; }
        }
        public string ServerVersion
        {
            get { return m_serverVersion.ToString(); }
            set { m_serverVersion = new Version(value); }
        }

        public string AutoUpdateCountdown
        {
            get { return m_autoUpdateCountdown; }
            set { m_autoUpdateCountdown = value; }
        }

        public bool IsUpdateAvailable
        {
            get
            {
                return LocalVersion.CompareTo(m_serverVersion) < 0;
            }
        }
        public static UpdateConfig Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new UpdateConfig();
                }

                return m_instance;
            }
        }
        public delegate void UpdateConfigLoadedEventHandler();
        public event UpdateConfigLoadedEventHandler UpdateConfigLoadedEvent = null;
        public UpdateConfig()
        {
        }


        public void LoadConfig(Uri uri)
        {
            LoadConfig(uri, 60000);
        }
        public void LoadConfig(Uri uri, int timeout)
        {
            Thread webRequestThread = new Thread(delegate()
            {
                WebRequest wReq = WebRequest.Create(uri);
                wReq.Timeout = timeout;
                try
                {
                    UpdateConfig uc = null;
                    WebResponse wResp = wReq.GetResponse();
                    XmlSerializer serializer = new XmlSerializer(typeof(UpdateConfig));
                    uc = (UpdateConfig)serializer.Deserialize(wResp.GetResponseStream());
                    m_serverVersion = uc.m_serverVersion;
                    m_downloadUrl = uc.m_downloadUrl;
                    m_updateCommand = uc.m_updateCommand;
                    m_autoUpdateCountdown = uc.m_autoUpdateCountdown;
                    if (UpdateConfigLoadedEvent != null)
                    {
                        UpdateConfigLoadedEvent();
                    }
                }
                catch (Exception updateConfigException)
                {
                    //write to debug out
                }

            });
            webRequestThread.Start();
        }
    }
}
